/* ozofra 1a
 This file contains the complete definition
 of the SecretCode class. */

#include <vector>
#include "d_random.h"

class SecretCode 
{
  public :
  SecretCode (int codeLength, int codeRange);
  void checkCorrect(const std::vector<int> &guess, int &correctDigits,
		   int &correctLocations) const;

 private:
  std::vector<int> secretValue;

  //Called by checkCorrect
  int checkDigits (const std::vector<int> &guess) const;  
  int checkLocations (const std::vector<int> &guess) const;  
};

SecretCode::SecretCode (int codeLength, int codeRange)
{
  randomNumber my_ran;

  //push random stuff n times
  for (int time=0; time<codeLength; time++)
  {
    int r_int = my_ran.random(codeRange);
    secretValue.push_back(r_int);
  }

}

void SecretCode::checkCorrect (const std::vector<int> &guess, 
			       int &correctDigits,
			       int &correctLocations) const 
// Returns the number of correctDigits and correctLocations
// using the private member functions
{
  correctDigits = checkDigits(guess);
  correctLocations = checkLocations(guess);
}

int SecretCode::checkDigits (const std::vector<int> &guess) const
// Returns the number of correct digits 
// that are in the correct location
{
  int correct=0;

  // loop over each guess element
  for (int gIdx=0; gIdx<guess.size(); gIdx++)
    // if identical to the secret, incr correct
    if (guess[gIdx] == secretValue[gIdx])
      correct++;

  return correct;
} // end checkDigits

int SecretCode::checkLocations(const vector<int> &guess) const
//Find the number of digits that are correct but in the wrong place
{
  int correctDigits = 0;
  vector<int> dSecret = secretValue;
		
  // Remove those that are already correct
  // from future consideration
  for (int gIdx=0; gIdx<guess.size(); gIdx++)
    if (guess[gIdx] == secretValue[gIdx])
      dSecret.at(gIdx) = -1;

  // Loop over each guess
  for (int gIdx = 0; gIdx < guess.capacity(); gIdx++)
  {

    // Loop over each secret term
    for	(int idx = 0; idx < dSecret.capacity(); idx++)
    {	

      // If a secret term has been found, 
      // Increment correctDigits
      if (dSecret.at(idx) == guess.at(gIdx))
      {
	dSecret.at(idx) = -1;
	correctDigits++;
	break;
      }

    } //end secret for loop

  } //end guess for loop

  return correctDigits;
} // end checkLocations
